package com.hsyt.pms.controller.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.hsyt.pfc.support.util.EnumToDataUtil;
import com.hsyt.pfc.support.util.LoggerUtil;
import com.hsyt.pms.api.common.PltLog;
import com.hsyt.pms.api.common.ResultMessage;
import com.hsyt.pms.api.dto.org.OrgDto;
import com.hsyt.pms.api.dto.system.SysFunctionDto;
import com.hsyt.pms.api.dto.system.SysRoleDto;
import com.hsyt.pms.api.enums.OrgType;
import com.hsyt.pms.api.enums.Status;
import com.hsyt.pms.api.vo.system.TreeNode;
import com.hsyt.pms.controller.base.BaseController;
import com.hsyt.pms.sys.dubbo.SysFunctionService;
import com.hsyt.pms.sys.dubbo.SysRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: SysGrantAuthController
 * @Description: 数据赋权服务控制层
 * @author: wangze@hsyuntai.com
 * @date: 2019/1/29 18:19
 */
@RestController
@RequestMapping(value = "/pms/system/grant")
public class SysGrantAuthController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysGrantAuthController.class);

    @Reference(group = "pmsSystem", registry = "pms", version = "0.0.1")
    private SysRoleService sysRoleService;

    @Reference(group = "pmsSystem", registry = "pms", version = "0.0.1")
    private SysFunctionService sysFunctionService;


    /**
     * 动态加载子节点（通用方法）
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/org/loadNode/{roleId}")
    @ResponseBody
    public ResultMessage dynamicLoadTreeNode(@PathVariable("roleId") Long roleId, HttpServletRequest request, @RequestBody OrgDto orgDto) {
        ResultMessage result = new ResultMessage(true, "成功");
        try {
            List<TreeNode> treeNode = sysRoleService.getOrgTree(null , orgDto.getRoleId(), orgDto);
            result.setContent(JSON.toJSONString(treeNode));
        } catch (Exception e) {
            result.change(false, "获取失败,错误信息:" + e.getMessage());
        }
        return result;
    }

    /**
     * 菜单功能赋权
     * 
     * @param model
     * @return
     */
    @RequestMapping(value = "/function/page")
    public ModelAndView functionPage(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(Status.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("grant/pageFunctionRole");
    }

    /**
     * 获取菜单功能点树
     * 
     * @param request
     * @return
     */
    @RequestMapping(value = "/function/tree")
    @ResponseBody
    public void functionTree(HttpServletRequest request, HttpServletResponse response, @RequestBody SysFunctionDto dto)  throws IOException {
        List<TreeNode> treeNode = new ArrayList<TreeNode>();
        try {
            treeNode = sysFunctionService.getMenus(null, dto);
        } catch (Exception e) {
            LoggerUtil.error(LOGGER, "获取菜单功能树错误 error: {0}", e.getMessage());
        }
        response.getWriter().write(JSON.toJSONString(treeNode));
    }

    /**
     * 消防组织赋权
     * 
     * @param model
     * @return
     */
    @RequestMapping(value = "/xiaofang/page")
    public ModelAndView xiaofangPage(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(Status.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("grant/pageXiaoFangRole");
    }

    /**
     * 消防组织功能点树
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/xiaofang/tree")
    @ResponseBody
    public void xiaofangTree(HttpServletRequest request, HttpServletResponse response, @RequestBody OrgDto orgDto) throws IOException {
        List<TreeNode> treeNode = new ArrayList<TreeNode>();
        try {
            orgDto.setOrgType(OrgType.FIRE.getValue());
            treeNode = sysRoleService.getOrgTree(null, orgDto.getRoleId(), orgDto); 
        } catch (Exception e) {
            LoggerUtil.error(LOGGER, "获取消防组织功能点树错误 error: {0}", e.getMessage());
        }
        response.getWriter().write(JSON.toJSONString(treeNode));
    }


    /**
     * 社会单位赋权
     * 
     * @param model
     * @return
     */
    @RequestMapping(value = "/shehui/page")
    public ModelAndView shehuiPage(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(Status.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("grant/pageSheHuiRole");
    }

    /**
     * 社会单位树
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/shehui/tree")
    @ResponseBody
    public void shehuiTree(HttpServletRequest request, HttpServletResponse response, @RequestBody OrgDto orgDto) throws IOException {
        List<TreeNode> treeNode = new ArrayList<TreeNode>();
        try {
            orgDto.setOrgType(OrgType.SOCIAL.getValue());
            treeNode = sysRoleService.getOrgTree(null , orgDto.getRoleId(), orgDto); 
        } catch (Exception e) {
            LoggerUtil.error(LOGGER, "获取社会单位树错误 error: {0}", e.getMessage());
        }
        response.getWriter().write(JSON.toJSONString(treeNode));
    }

    /**
     * 行业主管赋权
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/hangye/page")
    public ModelAndView hangyePage(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(Status.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("grant/pageHangYeRole");
    }

    /**
     * 行业主管树
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/hangye/tree")
    @ResponseBody
    public void hangyeTree(HttpServletRequest request, HttpServletResponse response, @RequestBody OrgDto orgDto) throws IOException {
        List<TreeNode> treeNode = new ArrayList<TreeNode>();
        try {
            orgDto.setOrgType(OrgType.INDUSTRY.getValue());
            treeNode = sysRoleService.getOrgTree(null , orgDto.getRoleId(), orgDto); 
        } catch (Exception e) {
            LoggerUtil.error(LOGGER, "获取行业主管树错误 error: {0}", e.getMessage());
        }
        response.getWriter().write(JSON.toJSONString(treeNode));
    }

    /**
     * 商户组织赋权
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/shanghu/page")
    public ModelAndView shanghuPage(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(Status.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("grant/pageShangHuRole");
    }

    /**
     * 商户组织树
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/shanghu/tree")
    @ResponseBody
    public void shanghuTree(HttpServletRequest request, HttpServletResponse response, @RequestBody OrgDto orgDto) throws IOException {
        List<TreeNode> treeNode = new ArrayList<TreeNode>();
        try {
            orgDto.setOrgType(OrgType.BUSINESS.getValue());
            treeNode = sysRoleService.getOrgTree(null , orgDto.getRoleId(), orgDto); 
        } catch (Exception e) {
            LoggerUtil.error(LOGGER, "获取商户组织树错误 error: {0}", e.getMessage());
        }
        response.getWriter().write(JSON.toJSONString(treeNode));
    }

    /**
     * 物业组织赋权
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/wuye/page")
    public ModelAndView wuyePage(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(Status.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("grant/pageWuYeRole");
    }

    /**
     * 物业组织树
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/wuye/tree")
    @ResponseBody
    public void wuyeTree(HttpServletRequest request, HttpServletResponse response, @RequestBody OrgDto orgDto) throws IOException {
        List<TreeNode> treeNode = new ArrayList<TreeNode>();
        try {
            orgDto.setOrgType(OrgType.TENEMENT.getValue());
            treeNode = sysRoleService.getOrgTree(null , orgDto.getRoleId(), orgDto); 
        } catch (Exception e) {
            LoggerUtil.error(LOGGER, "获取物业组织树错误 error: {0}", e.getMessage());
        }
        response.getWriter().write(JSON.toJSONString(treeNode));
    }

    /**
     * 受理中心赋权
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/shouli/page")
    public ModelAndView shouliPage(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(Status.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("grant/pageShouLiRole");
    }

    /**
     * 受理中心树
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/shouli/tree")
    @ResponseBody
    public void shouliTree(HttpServletRequest request, HttpServletResponse response, @RequestBody OrgDto orgDto) throws IOException {
        List<TreeNode> treeNode = new ArrayList<TreeNode>();
        try {
            orgDto.setOrgType(OrgType.ACCEPT_CENTER.getValue());
           treeNode = sysRoleService.getOrgTree(null , orgDto.getRoleId(), orgDto); 
        } catch (Exception e) {
            LoggerUtil.error(LOGGER, "获取受理中心树错误 error: {0}", e.getMessage());
        }
        response.getWriter().write(JSON.toJSONString(treeNode));
    }

    /**
     * 运维组织赋权
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/yunwei/page")
    public ModelAndView yunweiPage(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(Status.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("grant/pageYunWeiRole");
    }

    /**
     * 运维组织树
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/yunwei/tree")
    @ResponseBody
    public void yunweiTree(HttpServletRequest request, HttpServletResponse response, @RequestBody OrgDto orgDto) throws IOException {
        List<TreeNode> treeNode = new ArrayList<TreeNode>();
        try {
            orgDto.setOrgType(OrgType.MAINTENANCE.getValue());
            treeNode = sysRoleService.getOrgTree(null , orgDto.getRoleId(), orgDto); 
        } catch (Exception e) {
            LoggerUtil.error(LOGGER, "获取运维组织树错误 error: {0}", e.getMessage());
        }
        response.getWriter().write(JSON.toJSONString(treeNode));
    }

    /**
     * 微型消防赋权
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/weixing/page")
    public ModelAndView weixingPage(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(Status.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("grant/pageWeiXingRole");
    }

    /**
     * 微型消防树
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/weixing/tree")
    @ResponseBody
    public void weixingTree(HttpServletRequest request, HttpServletResponse response, @RequestBody OrgDto orgDto) throws IOException {
        List<TreeNode> treeNode = new ArrayList<TreeNode>();
        try {
            orgDto.setOrgType(OrgType.MICRO_FIRE.getValue());
            treeNode = sysRoleService.getOrgTree(null , orgDto.getRoleId(), orgDto); 
        } catch (Exception e) {
            LoggerUtil.error(LOGGER, "获取微型消防树错误 error: {0}", e.getMessage());
        }
        response.getWriter().write(JSON.toJSONString(treeNode));
    }

    /**
     * 建筑物赋权
     * 
     * @param model
     * @return
     */
    @RequestMapping(value = "/jianzhu/page")
    public ModelAndView jianzhuPage(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(Status.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("grant/pageJianZhuRole");
    }

    /**
     * 建筑物树
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/jianzhu/tree")
    @ResponseBody
    public ResultMessage jianzhuTree(HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        try {
            List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
            // TODO 获取树数据
            result.setContent("");
        } catch (Exception e) {
            result.change(false, "获取失败,错误信息:" + e.getMessage());
        }
        return result;
    }

    /**
     * 设备点赋权
     * 
     * @param model
     * @return
     */
    @RequestMapping(value = "/shebeidian/page")
    public ModelAndView shebeidianPage(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(Status.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("grant/pageSheBeiDianRole");
    }

    /**
     * 设备点树
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/shebeidian/tree")
    @ResponseBody
    public ResultMessage shebeidianTree(HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        try {
            List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
            // TODO 获取树数据
            result.setContent("");
        } catch (Exception e) {
            result.change(false, "获取失败,错误信息:" + e.getMessage());
        }
        return result;
    }

    /**
     * 设备区赋权
     * 
     * @param model
     * @return
     */
    @RequestMapping(value = "/shebeiqu/page")
    public ModelAndView shebeiquPage(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(Status.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("grant/pageSheBeiQuRole");
    }

    /**
     * 设备区树
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/shebeiqu/tree")
    @ResponseBody
    public ResultMessage shebeiquTree(HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        try {
            List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
            // TODO 获取树数据
            result.setContent("");
        } catch (Exception e) {
            result.change(false, "获取失败,错误信息:" + e.getMessage());
        }
        return result;
    }

    /**
     * 二次赋权
     * 
     * @param model
     * @return
     */
    @RequestMapping(value = "/erci/page")
    public ModelAndView erciPage(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(Status.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("grant/pageErCiRole");
    }

    /**
     * 二次树
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/erci/tree")
    @ResponseBody
    public ResultMessage erciTree(HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        try {
            List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
            // TODO 获取树数据
            result.setContent("");
        } catch (Exception e) {
            result.change(false, "获取失败,错误信息:" + e.getMessage());
        }
        return result;
    }

    @PltLog(log = "保存角色与组织资源关系")
    @RequestMapping(value = "/saveOrgRole")
    public @ResponseBody
    ResultMessage saveOrgRole(@RequestBody SysRoleDto sysRoleDto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        try {
            sysRoleService.saveOrgRole(sysRoleDto);
        } catch (Exception e) {
            result.change(false, "保存失败,错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "saveOrgRole error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "保存角色与建筑物资源关系")
    @RequestMapping(value = "/saveSourceRole")
    public @ResponseBody
    ResultMessage saveSourceRole(@RequestBody SysRoleDto sysRoleDto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        try {
            sysRoleService.saveSourceRole(sysRoleDto);
        } catch (Exception e) {
            result.change(false, "保存失败,错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "Role-saveBuildingRole error: {0}", e.getMessage());
        }
        return result;
    }

}
